#### INSTALL THESE REQUIRED PACKAGES ####

# install and load "remotes" package so you can install specific versions of packages
install.packages("remotes")
library(remotes)

install_version("tidyverse", "2.0.0")
install_version("xtable", "1.8-4")
install_version("broom", "1.0.5")
install_version("estimatr", "1.0.2")
install_version("deming", "1.4")
install_version("rsample", "1.2.0")
install_version("ggpubr", "0.6.0")
install_version("randomForest", "4.7-1.1")
install_version("grf", "2.3.2")
install_version("gt", "0.10.1")
install_version("gtsummary", "1.7.2")
install_version("radiant", "1.6.1")
install_version("rio", "1.0.1")
install_version("datawizard", "0.9.1")
install_version("Hmisc", "5.1-1")

### Make sure your working directory is set to the folder containing "master.R".
### The following code is meant to be run in order.

# Sources "prepping_50_cleaned_datasets.R" which reads in original raw data and
# constructs variables we use in our analysis and selects profile variables. Outputs 
# datastes to data/analysis/cleaned.
source("code/data_processing/prepping_50_cleaned_datasets.R")
setwd("../../") # returns WD to folder with master.R

# Sources "compiling_pooled_dataset.R" which binds rows from all 50 studies
# into one "pooled" dataset. Outputs to data/analysis.
source("code/data_processing/compiling_pooled_dataset.R")
setwd("../../../") # returns WD to folder with master.R

##### Table 1 #####
# Sources "Table_1.R" which builds and outputs "Table_1_counts.tex" and
# "Table_1_proportions.tex" in ./results.
source("code/analysis/Table_1.R")
setwd("../../") # returns WD to folder with master.R

##### Figure 2 and Table J6 #####
# Sources "Figure_2+Table_J6.R" which builds and outputs Figure 2 and Table J6 in ./results.
source("code/analysis/Figure_2+Table_J6.R")
setwd("../../../") # returns WD to folder with master.R

##### Figure 3 and Table F3 #####
# Sources "Figure_3+Table_F3.R" which builds and outputs Figure 3 and Table F3 in ./results.
source("code/analysis/Figure_3+Table_F3.R")
setwd("../../../") # returns WD to folder with master.R

##### Figure 4 and Table I5 #####
# Sources "Figure_4+Table_I5.R" which builds and outputs Figure 4 and Table I5 in ./results.
source("code/analysis/Figure_4+Table_I5.R")
setwd("../../") # returns WD to folder with master.R

##### Table 2 and Figure H5 #####
# Sources "Table_2+Figure_H5.R" which builds and outputs Figure H5 in ./results.
# It estimates the models and prints the associated output for Table 2 in the console.
source("code/analysis/Table_2+Figure_H5.R")
setwd("../../") # returns WD to folder with master.R

# Sources "Table_2_Row_3.R" which estimates the models and prints 
# the associated output for Table 2 in the console.
source("code/analysis/Table_2_Row_3.R")
setwd("../../") # returns WD to folder with master.R

##### Table A1 #####
# Sources "Table_A1.R" which builds and outputs Table A1 in ./results.
source("code/analysis/Table_A1.R")
setwd("../../") # returns WD to folder with master.R

##### Figures G1 and G2 and Table G4 #####
# Sources "Figures_G1+G2+Table_G4" which builds and outputs Figure G1, Figure G2
# and Table G4 in ./results.
source("code/analysis/Figures_G1+G2+Table_G4.R")
setwd("../../../") # returns WD to folder with master.R

##### Figure H3 #####
# Sources "Figure_H3.R" which builds and outputs Figure H3 in ./results.
source("code/analysis/Figure_H3.R")
setwd("../../../") # returns WD to folder with master.R

##### Figure H4 #####
# Sources "Figure_H4.R" which builds and outputs Figure H4 in two output files,
# "Figure_H4_pt1.pdf" and "Figure_H4_pt2.pdf" in ./results.
source("code/analysis/Figure_H4.R")
setwd("../../../") # returns WD to folder with master.R

